package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.GetIhomeBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.InfoSearchAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import de.greenrobot.event.EventBus;

public class InfoSearchActivity extends BaseActivity {

    InfoSearchAdapter mAdapter;
    UserPresenter mPresenter;
    List<GetIhomeBean.DataEntity> mList;
    int typeid;
    int page = 1;

    @Bind(R.id.et_info_search)
    EditText mEtInfoSearch;
    @Bind(R.id.rl_search_info)
    RecyclerView mRlSearchInfo;
    @Bind(R.id.tv_cancel)
    TextView mTvCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_search);
        ButterKnife.bind(this);

        initIntent();
        initSearchList();

        search();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
        mPresenter = new UserPresenterImp();
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initIntent() {
        Intent intent = getIntent();
        typeid = intent.getIntExtra(Constant.SEARCH_INFO_TYPE, 0);
    }

    private void initSearchList() {
        mList = new ArrayList<>();
        mAdapter = new InfoSearchAdapter(mList);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRlSearchInfo.setLayoutManager(manager);
        mRlSearchInfo.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new InfoSearchAdapter.OnItemClickListener() {
            @Override
            public void click(String infoId) {
                Intent intent = new Intent(InfoSearchActivity.this, InfoDetailActivity.class);
                intent.putExtra(Constant.INFO_DETAIL, infoId);
                startActivity(intent);
            }
        });
    }

    private void search() {
        mEtInfoSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    mList.clear();
                    mAdapter.notifyDataSetChanged();
                } else {
                    mPresenter.getIhomeSearch(String.valueOf(typeid), page, s.toString());
                }
            }
        });
    }

    public void onEventMainThread(GetIhomeBean bean) {
        mList.clear();
        if (bean.getReturn_code() == 0) {
            mList.addAll(bean.getData());
            mAdapter.notifyDataSetChanged();
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }

    @OnClick(R.id.tv_cancel)
    public void onClick() {
        finish();
    }
}
